package day240920;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/download-html")
public class DownloadHtmlServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置内容类型为HTML
        response.setContentType("text/html");

        // 设置HTTP头，使文件可下载，并指定下载的文件名
        response.setHeader("Content-Disposition", "attachment; filename=\"downloaded.html\"");

        // 获取响应的输出流
        PrintWriter out = response.getWriter();

        // 写入HTML内容
        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("<title>Downloaded HTML Page</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>This is a downloadable HTML page</h1>");
        out.println("<p>Content of the page goes here...</p>");
        out.println("</body>");
        out.println("</html>");

        // 关闭输出流
        out.close();
    }
}